package com.saishi.servlet;

import com.saishi.dao.UserDao;
import com.saishi.dao.impl.UserDaoImpl;
import com.saishi.entity.Classes;
import com.saishi.entity.User;
import com.saishi.entity.uclass;

//import javafx.scene.control.Alert;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sound.midi.Soundbank;
import javax.swing.*;
import java.io.Console;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/user/*")
public class StudentGlServlet extends BaseServlet {
    UserDao dao = new UserDaoImpl();

    protected void all(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        try {
            List list = dao.allQuery();
            System.out.println(list);
            HttpSession session = request.getSession();
            session.setAttribute("user", list);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.getRequestDispatcher("/xsgl.jsp").forward(request, response);

    }

    protected void addxs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String sno = request.getParameter("sno");
        String nickname = request.getParameter("nickname");
        String pass = request.getParameter("pass");
        String sname = request.getParameter("sname");
        String ssex = request.getParameter("ssex");
        String age = request.getParameter("age");
        String tel = request.getParameter("tel");
        String email = request.getParameter("email");
        String job = request.getParameter("job");
        String address = request.getParameter("address");
        String state = request.getParameter("state");
//        String pre = request.getParameter("previewimg");
        int uid = Integer.parseInt(sno);
        int uage = Integer.parseInt(age);
        User user = new User(uid, nickname, pass, sname, tel, email, ssex, uage, job, address, state, null);
        System.out.println(user);
        try {
            int rs = dao.adduid(user);
            if (rs > 0) {
                System.out.println(1);
                response.sendRedirect("/user/all");
            } else {
                System.out.println(2);
                response.sendRedirect("/");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        int sno = Integer.parseInt(request.getParameter("uid"));
        System.out.println(sno);
        try {
            int result = dao.del(sno);
            if (result > 1) {
                request.setAttribute("message", "删除成功");
            } else {
                request.setAttribute("message", "删除失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        response.sendRedirect("/user/all");
    }

    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String sno = request.getParameter("sno");
        String nickname = request.getParameter("nickname");
        String pass = request.getParameter("pass");
        String sname = request.getParameter("sname");
        String ssex = request.getParameter("ugender");
        String age = request.getParameter("age");
        String tel = request.getParameter("tel");
        String email = request.getParameter("email");
        String job = request.getParameter("job");
        String address = request.getParameter("address");
        String state = request.getParameter("state");
//        String pre = request.getParameter("previewimg");
        int uid = Integer.parseInt(sno);
        int uage = Integer.parseInt(age);

        User user = new User(uid, nickname, pass, sname, tel, email, ssex, uage, job, address, state, null);
        try {
            dao.update(user);
            request.getRequestDispatcher("/user/all").forward(request, response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    protected void chanall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String sno = request.getParameter("uid");
        System.out.println(sno);
        int uid = Integer.parseInt(sno);
        uclass user = dao.findclass(uid);
        List classes = dao.chefind();
        System.out.println(user);
        request.setAttribute("uclass", user);
        request.setAttribute("clas", classes);
        request.getRequestDispatcher("/Changbj.jsp").forward(request, response);
    }

    protected void upclas(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String uid = request.getParameter("clason");
        System.out.println(uid);
        int uuid = Integer.parseInt(uid);
        String sgrade = request.getParameter("sgrade");
        try {
            int user = dao.upbj(uuid,sgrade);
            request.getRequestDispatcher("/user/all").forward(request,response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}